<?php
/**
 * 消息模板
 * Created by PhpStorm.
 * User:
 * Date: 2017/4/26
 * Time: 15:07
 */

namespace App\Services;


use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;

use Curl\Curl;

trait UploadServices
{
    use BaseServices;
    /**
     * 下发消息
     *
     * @param $data  [park_code,start_time,end_time,msgno,msg,mode]
     * @return json
     */
    public function uploadImgToAttachmentServer($filename = '')
    {
        if (empty($filename)) {
            return '';
        }

        $prefix = config('custom.attachment_server_url');
        $url    = config('custom.attachment_api_server');
        $token  = config('custom.attachment_api_token');

        $curl = new Curl;
        $curl->setHeader('Content-Type', 'multipart/form-data');
        $data = [
            'token' => $token,
            'file'  => sprintf('@%s', $filename),
        ];


        $curl->post($url, $data);

        if ($curl->errorCode != 0) {
            Log::info('UPLOAD.FIALED: '. $filename);
            return '';
        }

        $rs = $curl->response;

        // success
        if ($rs && $rs->status ==0) {
            return str_ireplace($prefix, '', $rs->data);
        }

        Log::info('UPLOAD.FIALED.RETURN: '. $rs->msg);
        return '';
    }

}